import type { ExportGroup, ExportSetting, ExportTableSetting } from "@/utils/struct";
import { id } from "element-plus/es/locales.mjs";
import { defineStore } from "pinia";

export const useSettingStore = defineStore("setting", {
    state: () => {
        return {
            groupIdMax:2,
            exportGroups:[] as ExportGroup[],
            exportSettings:[] as ExportSetting[],
            nextExportSettingId:1
        };
    },
    actions: {
        initData(data:any) {
            this.groupIdMax = data.groupIdMax;
            this.exportGroups.splice(0, this.exportGroups.length)
            this.exportGroups.push(...data.exportGroups);
            
            this.nextExportSettingId = data.settingNextId || 1;
            this.exportSettings.splice(0, this.exportSettings.length)
            if(data.exportSettings){
                this.exportSettings.push(...data.exportSettings);
            }
        },
        getExportGroupIds() {
            let ids = []
            for (let i = 0; i < this.exportGroups.length; i++) {
                ids.push(this.exportGroups[i].id);
            }
            return ids;
        },
        getExportGroup(id:number) {
            return this.exportGroups.find((item: ExportGroup) => item.id === id);
        },
        getExportGroupName(id:number) {
            let group = this.getExportGroup(id);
            return group == null ? "???" : group.name;
        },
        addGroup(name:string, desc:string = "") {
            this.exportGroups.push({
                id:++this.groupIdMax,
                name:name,
                desc:desc
            });
        },
        
        // 导出设置相关方法
        getExportSettings() {
            return this.exportSettings;
        },
        
        getExportSetting(id:number) {
            return this.exportSettings.find(setting => setting.id === id);
        },
        
        addExportSetting() {
            // 确保设置包含name和desc属性
            let newSetting = {
                id: this.nextExportSettingId,
                name: '未命名设置',
                desc: '',
                exportMethod: 'json',
                tableSettings: [
                ]
            };
            this.nextExportSettingId++;
            this.exportSettings.push(newSetting);
        },
        
        updateExportSetting(index:number, setting: Partial<ExportSetting>) {
            if(index >=0 && index < this.exportSettings.length){
                this.exportSettings[index] = {
                    ...this.exportSettings[index],
                    ...setting
                };
            }
        },
        
        deleteExportSetting(index:number) {
            if(index >=0 && index < this.exportSettings.length){
                this.exportSettings.splice(index, 1);
            }
        },
        addTableSetting(exportSettingId:number, tableId:number) {
            let exportSetting = this.exportSettings.find(s => s.id === exportSettingId);
            if (!exportSetting) return;
            let tableSetting = {
                tableId: tableId,
                exportColumnIds: [],
                subTableSettings: [],
                dictKeyValues: []
            } as ExportTableSetting
            exportSetting.tableSettings.push(tableSetting);
        }
    },
});